package controller.game
{
	import controller.BaseController;
	import controller.ControllerManager;
	import controller.SocketManager;
	import controller.events.SocketEvent;
	import controller.game.GameObjectManager;
	import controller.login.LoginController;
	import controller.room.RoomController;
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	import json.*;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.controls.Label;
	import mx.core.FlexGlobals;
	import mx.flash.UIMovieClip;
	import mx.formatters.DateFormatter;
	
	import poker.LevelDefine;
	
	import spark.components.Button;
	import spark.components.Group;
	import spark.components.Image;
	
	public class GameController extends BaseController
	{
		// static instance 
		public static var instance:GameController = null;
		
		public var gameId:String;		
		
		// 总牌数
		public static const cardsAmount:int = 54;
		
		// 三张底牌的中心基准点
		public static const cardThreeStandardX:int = 500;//258; //自己手牌x
		public static const cardThreeStandardY:int = 10;//50;//自己手牌y
		// 玩家的牌的中心基准点
		public static const cardStandardX:int = 350;//258; //自己手牌x
		public static const cardStandardY:int = 450;//50;//自己手牌y
		public static const playedCardStdX:int = 400;
		public static const playedCardStdY:int = 300;
		// 左边玩家的位置，
		private static const leftCardback_x:int =120;// 15; //zuo shou x 
		private static const leftCardback_y:int =210;// 332;
		private static const playedleftCardStdX:int = 200;
		private static const playedleftCardStdY:int =210// 236;//zuo shou y
	
		// 右边玩家的位置
		private static const rightCardback_x:int = 600;//474; //youshou x
		private static const rightCardback_y:int =210;// 332; 
		private static const playedrightCardStdX:int = 520;
		private static const playedrightCardStdY:int = 210;//youshou y
		// 牌堆类型1的宽高
		private static const cardback1_w:int = 56;//61;
		private static const cardback1_h:int = 76;//95;
		
		// 记录玩家的牌的数组
		// 关于玩家的牌的顺序的定义如下：
		// 按照花色的顺序从 方块-》梅花-》红桃-》黑桃 数字从3开始，然后是4
		// 举例如： 33334444555566667777。。。KKKKAAAA2222
		public var PlayerCards:Array = new Array();//当前牌数组
		
		public var ThreeCardsObject:Array = new Array();
		public var PlayerCardsObject:Array = new Array();
		public var PlayerLastPlayedCards:Array = new Array();
		private var PlayerCardsLeft:Array = new Array();
		private var PlayerCardsRight:Array = new Array();
		
		// 定义所有的牌在整图中的位置
		// 所有的牌的定义，按照花色的顺序从 方块-》梅花-》红桃-》黑桃
		// 牌值从3开始到2结束,所以是3333，4444，kkkk，AAAA，然后2222的顺序
		// 最后的52，53是joker--司令。
		private static var cardsRect:ArrayCollection = new ArrayCollection();
		// 牌的排列间隔
		public static const cardsWidth:int = 61;
		public static const cardsHeight:int = 86;
		public static const cardsIntervalX:int = 12;
		public static const cardsIntervalY:int = 16;
		
		// 当前出牌玩家的座位号
		public var curPos:int;
		public var curPlayerLast:int;
		// 这个指最后出牌的玩家编号
		public var lastPos:int;
		public var mutil:int=1;
		//游戏中细分状态，以便给taskloop执行任务
		//1 叫分 2，出牌
		public var gamestate:int;
		public var callScoreCount:int=0;
		public var isCallScore:Boolean=false;
		public var isClock:Boolean=false;
		public var mustSend:Boolean=false;
		public var isMyTurn:Boolean=false;
		// 记录其他玩家出的牌
		// 按照
		private var deskCards1:Array = new Array();
		private var deskCards2:Array = new Array();
		private var deskCards3:Array = new Array();
		private var deskCards1LastAmount:int;
		private var deskCards2LastAmount:int;
		private var deskCards3LastAmount:int;
		
		
		//////////////////////////////////////////////////////////////////////////////
		// card height ZOrder
		private const cardback_BaseZOrder:int	 = 100;
		private const BG_BaseZOrder:int = 0;
		private const cardplayed1_BaseZOrder:int	 = 220;
		private const cardplayed2_BaseZOrder:int	 = 240;
		private const cardplayed3_BaseZOrder:int	 = 260;
		private const card_BaseZOrder:int = 500;
		
		// 用来标识玩家是否使用了托管
		public var isCpuAI:Boolean = false;
		// 计时器
		private var lastFrameTime:Date = new Date();
		// 游戏中玩家的倒计时时间值
		public var gamePlayerLeftTimeDef:int = 30;
		private var gamePlayerLeftTimeCounter:int = -1;
		private var gpltStartTime:Date = new Date();
		private var gpltPlusTime:int = 0;
		public var timerCallBackFun:Function=null;
		
		// 用来记录游戏中提示用的数据
		private var lastCard:Array = new Array();	
		private var lastCkeckCard:Array = new Array();
		private var hintCards:Array = new Array();
		private var hintTimes:int = 0;
		public function GameController()
		{
			super();
			if ( instance != null )
				throw new Error( "Only one Singleton instance should be instantiated" ); 
		}
		
		public function taskLoop():void{
			// Calculate the time since the last frame
			var thisFrame:Date = new Date();
			var seconds:Number = (thisFrame.getTime() - lastFrameTime.getTime())/1000.0;
			switch(this.state){
				case 0://登陆界面
					break;
				case 1://大厅
					break;
				case 2://加入房间
					break;
				case 3://已准备
					break;
				case 4://游戏开始，发牌					
					if(isClock){
						updatePlayerLeftTime();
					}					
					GameObjectManager.Instance.enterFrame();
					break;
			}
		}
		public function updatePlayerLeftTime():void
		{
			if(gamePlayerLeftTimeDef != -1)
			{	// 每帧都进行时间的计算，并自动的进行计时
				var thisFrame:Date = new Date();
				var difference:Number = (thisFrame.getTime() - gpltStartTime.getTime())/1000.0;
				gamePlayerLeftTimeCounter = gamePlayerLeftTimeDef - difference + gpltPlusTime;	
				var count10:int=Math.floor(gamePlayerLeftTimeCounter/10);
				var count1:int=gamePlayerLeftTimeCounter%10;
				if(count10<0){
					count10=0;
				}
				if(count1<0){
					count1=0;
				}
				FlexGlobals.topLevelApplication.clocltime_count10.source=ResourceManagerPoker.gameNumberRes[count10];
				FlexGlobals.topLevelApplication.clocltime_count1.source=ResourceManagerPoker.gameNumberRes[count1];
				// 计时时间到了，进行一次连接请求
				if(gamePlayerLeftTimeCounter <= 0)
				{
					//做动作，首出：自动出牌，轮到，pass
					
					//隐藏时钟，取消监听事件
					FlexGlobals.topLevelApplication.ClockTimeInfo.visible=false;
					gamePlayerLeftTimeCounter = 0;
					isClock=false;
					// 这个立即更新，可能会因为前一次的更新没有完成而产生等待，并不是真正意义上的立即更新
					gpltStartTime=new Date();
					if(this.timerCallBackFun!=null){
						timerCallBackFun();
					}
				}
//				FlexGlobals.topLevelApplication.label_leftTimeCounter.text = gamePlayerLeftTimeCounter.toString();
			}
		}
		public function updateImmediately():void
		{
//			lastFrameTime = new Date();
		}
		// 当前游戏状态
		public function get state():int{
			return controller.ControllerManager.Instance.state;
		}
		// 玩家的座位号
		public function get selfPos():int{
			return controller.room.RoomController.instance.pos;
		}
		// 左边玩家的座位号
		public function get leftpos():int{
			return controller.room.RoomController.instance.getplayerpos(true);
		}
		// 右边玩家的座位号
		public function get rightpos():int{
			return controller.room.RoomController.instance.getplayerpos(false);
		}
		static public function get Instance():GameController
		{
			if ( instance == null )
				instance = new GameController();
			return instance;
		}	
	/*	public override function init():void{
			//初始化被通知派发事件
//			initEvents();
		}*/
		public function removeEventsWhenExitRoom():void{
			removeEvents();
		}		
	
		//游戏开始，注册游戏服务端主动通知，游戏创建，游戏等待，游戏开始，第一个出牌
		public function initEvents():void{			
			SocketManager.Instance.addEventListener(SocketEvent.GAME_GAME_CREATE,GAME_GAME_CREATE_HANDLER);
			SocketManager.Instance.addEventListener(SocketEvent.GAME_GAME_WAIT,GAME_GAME_WAIT_HANDLER);
			SocketManager.Instance.addEventListener(SocketEvent.GAME_GAME_STARTED,GAME_GAME_STARTED_HANDLER);
			SocketManager.Instance.addEventListener(SocketEvent.GAME_FIRST_PLAY,GAME_FIRST_PLAY_HANDLER);
			SocketManager.Instance.addEventListener(SocketEvent.GAME_INTERRUPTED_OTHER_DISCONNECT,GAME_INTERRUPTED_OTHER_DISCONNECT_HANDLER);
			SocketManager.Instance.addEventListener(SocketEvent.GAME_CALL_SCORE_NOTIFY,GAME_CALL_SCORE_NOTIFY_HANDLER);
			SocketManager.Instance.addEventListener(SocketEvent.GAME_BE_LANDLORD_NOTIFY,GAME_BE_LANDLORD_NOTIFY_HANDLER);
			SocketManager.Instance.addEventListener(SocketEvent.GAME_MUST_SEND_NOTIFY,GAME_MUST_SEND_NOTIFY_HANDLER);
			SocketManager.Instance.addEventListener(SocketEvent.GAME_TURNTO_PALY_NOTIFY,GAME_TURNTO_PALY_NOTIFY_HANDLER);
			SocketManager.Instance.addEventListener(SocketEvent.GAME_RECEIVER_MESSAGE_NOTIFY,GAME_RECEIVER_MESSAGE_NOTIFY_HANDLER);
			SocketManager.Instance.addEventListener(SocketEvent.GAME_OVER_NOTIFY,GAME_OVER_NOTIFY_HANDLER);
			SocketManager.Instance.addEventListener(SocketEvent.GAME_WINNER_PRODUCED_NOTIFY,GAME_WINNER_PRODUCED_NOTIFY_HANDLER);
			
		}
		private function removeEvents():void{
			
			SocketManager.Instance.removeEventListener(SocketEvent.GAME_GAME_CREATE,GAME_GAME_CREATE_HANDLER);
			SocketManager.Instance.removeEventListener(SocketEvent.GAME_GAME_WAIT,GAME_GAME_WAIT_HANDLER);
			SocketManager.Instance.removeEventListener(SocketEvent.GAME_GAME_STARTED,GAME_GAME_STARTED_HANDLER);
			SocketManager.Instance.removeEventListener(SocketEvent.GAME_FIRST_PLAY,GAME_FIRST_PLAY_HANDLER);
			SocketManager.Instance.removeEventListener(SocketEvent.GAME_INTERRUPTED_OTHER_DISCONNECT,GAME_INTERRUPTED_OTHER_DISCONNECT_HANDLER);
			SocketManager.Instance.removeEventListener(SocketEvent.GAME_CALL_SCORE_NOTIFY,GAME_CALL_SCORE_NOTIFY_HANDLER);
			SocketManager.Instance.removeEventListener(SocketEvent.GAME_MUST_SEND_NOTIFY,GAME_MUST_SEND_NOTIFY_HANDLER);
			SocketManager.Instance.removeEventListener(SocketEvent.GAME_TURNTO_PALY_NOTIFY,GAME_TURNTO_PALY_NOTIFY_HANDLER);
			SocketManager.Instance.removeEventListener(SocketEvent.GAME_RECEIVER_MESSAGE_NOTIFY,GAME_RECEIVER_MESSAGE_NOTIFY_HANDLER);
			SocketManager.Instance.removeEventListener(SocketEvent.GAME_OVER_NOTIFY,GAME_OVER_NOTIFY_HANDLER);
			SocketManager.Instance.removeEventListener(SocketEvent.GAME_WINNER_PRODUCED_NOTIFY,GAME_WINNER_PRODUCED_NOTIFY_HANDLER);
		}
		private function GAME_OVER_NOTIFY_HANDLER(event:SocketEvent):void{
			
			controller.ControllerManager.Instance.debug("接收到派发消息：GAME_OVER_NOTIFY_HANDLER"+event.results[1]+",MSG:"+event.results[3],true,true);
			//{win,1:6,1total:10,2:-3,3:-3}
			if(this.receive(event.results)){
				//放弃提示
				FlexGlobals.topLevelApplication.imgDiscardLeft.visible=false;
				FlexGlobals.topLevelApplication.imgDiscardDown.visible=false;
				FlexGlobals.topLevelApplication.imgDiscardRight.visible=false;
				FlexGlobals.topLevelApplication.btnReady.visible=true;		
				FlexGlobals.topLevelApplication.GameCardsCommand.visible=false;
				FlexGlobals.topLevelApplication.ClockTimeInfo.visible=false;
				this.isClock=false;
				this.mustSend=false;
				ControllerManager.Instance.state=1;//join room,but not ready!
				this.timerCallBackFun=null;
				this.lastCard.slice(0);
				this.lastCkeckCard.splice(0);				
				//drawother hand card		
				trace("摊牌，清除左手玩家的桌牌");
				GameObjectManager.Instance.removeOthersfromScreen("HandCardLeft");
				// 对得到的牌进行排序并显示			
				trace("摊牌，左手玩家桌牌数："+PlayerCardsLeft.length);
				for(var k:int=0; k<PlayerCardsLeft.length; k++)
				{
					var pt:Point  = new Point(leftCardback_x,leftCardback_y-(PlayerCardsLeft.length*cardsIntervalY/2)+k*cardsIntervalY);
					// 传入的是左上的位置坐标
					//						GameObjectManager.Instance.setSpecCardVisible(PlayerCardsRight[k], "HandCardRight", pt, cardback_BaseZOrder+k, true);
					//
					trace("摊牌，绘制左手玩家的桌牌："+PlayerCardsLeft[k]);
					GameObjectManager.Instance.addCardtoScreen(PlayerCardsLeft[k], "HandCardLeft", pt);
				}
				FlexGlobals.topLevelApplication.Label_leftcardsnumLeft.text=this.PlayerCardsLeft.length+"张";
				
				GameObjectManager.Instance.removeOthersfromScreen("HandCardRight");
				trace("摊牌，清除右手玩家的桌牌");
				trace("摊牌，左手玩家桌牌数："+PlayerCardsRight.length);
				// 对得到的牌进行排序并显示
				PlayerCardsRight.reverse();
				for(var k:int=0; k<PlayerCardsRight.length; k++)
				{
					var pt:Point  = new Point(rightCardback_x,rightCardback_y-(PlayerCardsRight.length*cardsIntervalY/2)+k*cardsIntervalY);
					// 传入的是左上的位置坐标
					//						GameObjectManager.Instance.setSpecCardVisible(PlayerCardsRight[k], "HandCardRight", pt, cardback_BaseZOrder+k, true);
					//						pt = null;
					trace("摊牌，绘制右手玩家的桌牌："+PlayerCardsRight[k]);
					GameObjectManager.Instance.addCardtoScreen(PlayerCardsRight[k], "HandCardRight", pt);
				}
				FlexGlobals.topLevelApplication.Label_leftcardsnumRight.text=this.PlayerCardsRight.length+"张";
				
			}
		}
		private function GAME_WINNER_PRODUCED_NOTIFY_HANDLER(event:SocketEvent):void{			
			controller.ControllerManager.Instance.debug("接收到派发消息：GAME_WINNER_PRODUCED_NOTIFY_HANDLER"+event.results[1]+",MSG:"+event.results[3],true,true);
			//{win:3}
			if(this.receive(event.results)){
				FlexGlobals.topLevelApplication.GameCardsCommand.visible=false;
				this.timerCallBackFun=null;
				//drawother hand card		
				   trace("摊牌，清除左手玩家的桌牌");
					GameObjectManager.Instance.removeOthersfromScreen("HandCardLeft");
					// 对得到的牌进行排序并显示			
					trace("摊牌，左手玩家桌牌数："+PlayerCardsLeft.length);
					for(var k:int=0; k<PlayerCardsLeft.length; k++)
					{
						var pt:Point  = new Point(leftCardback_x,leftCardback_y-(PlayerCardsLeft.length*cardsIntervalY/2)+k*cardsIntervalY);
						// 传入的是左上的位置坐标
						//						GameObjectManager.Instance.setSpecCardVisible(PlayerCardsRight[k], "HandCardRight", pt, cardback_BaseZOrder+k, true);
						//
						trace("摊牌，绘制左手玩家的桌牌："+PlayerCardsLeft[k]);
						GameObjectManager.Instance.addCardtoScreen(PlayerCardsLeft[k], "HandCardLeft", pt,true);
					}
					FlexGlobals.topLevelApplication.Label_leftcardsnumLeft.text=this.PlayerCardsLeft.length+"张";
		
					GameObjectManager.Instance.removeOthersfromScreen("HandCardRight");
					trace("摊牌，清除右手玩家的桌牌");
					trace("摊牌，左手玩家桌牌数："+PlayerCardsRight.length);
					// 对得到的牌进行排序并显示
					PlayerCardsRight.reverse();
					for(var k:int=0; k<PlayerCardsRight.length; k++)
					{
						var pt:Point  = new Point(rightCardback_x,rightCardback_y-(PlayerCardsRight.length*cardsIntervalY/2)+k*cardsIntervalY);
						// 传入的是左上的位置坐标
						//						GameObjectManager.Instance.setSpecCardVisible(PlayerCardsRight[k], "HandCardRight", pt, cardback_BaseZOrder+k, true);
						//						pt = null;
						trace("摊牌，绘制右手玩家的桌牌："+PlayerCardsRight[k]);
						GameObjectManager.Instance.addCardtoScreen(PlayerCardsRight[k], "HandCardRight", pt,true);
					}
					FlexGlobals.topLevelApplication.Label_leftcardsnumRight.text=this.PlayerCardsRight.length+"张";
				
				
			}
		}
		
		//叫分，1,2,3，不叫,不需要服务器响应
		public function GAME_CALL_SCORE(event:MouseEvent):void
		{
//			SocketManager.Instance.addEventListener(SocketEvent.GAME_CALL_SCORE,GAME_CALL_SCORE_HANDLER);
			var button:Button=event.currentTarget as Button;
			var score:int=0;
			switch(button.id){
				case "GameScoreCommand3" :
					score=3;
					break;
				case "GameScoreCommand2" :
					score=2;
					break;
				case "GameScoreCommand1" :
					score=1;
					break;
				case "GameScoreCommandbujiao" :
					score=0;
					break;
			}
//			ShowClockAndCallBack(null);
			SocketManager.Instance.send(this.CreateOutMessage(BaseController.FIGHT_LANDLORD,"GAME_CALL_SCORE",score.toString()));
			FlexGlobals.topLevelApplication.GameScoreCommand.visible=false;
			callScoreCount++;
			
		}
		//出牌逻辑
		public function GAME_SEND_CARD(cards:String):void
		{
			//
			SocketManager.Instance.send(this.CreateOutMessage(BaseController.FIGHT_LANDLORD,"GAME_CALL_SCORE",cards));
			
		}
		private function GAME_BE_LANDLORD_NOTIFY_HANDLER(event:SocketEvent):void{
			//{"landlordpos":2,"threecards":"0V3,0V4,0V5","lastpost":1,"iscall":false,"score":0,maxcallscore:2,mutilple:1,islandlord:true}
			controller.ControllerManager.Instance.debug("接收到派发消息：GAME_BE_LANDLORD_NOTIFY_HANDLER"+event.results[1]+",MSG:"+event.results[3],true);
			if(this.receive(event.results)){
				this.isCallScore=false;
				this.lastCard.slice(0);
				FlexGlobals.topLevelApplication.GameScoreCommand.visible=false;
				this.curPos=this.result.landlordpos;
				//显示倒计时闹钟在正确位置，时间重设30，通知taskloop可以倒计时
			/*	FlexGlobals.topLevelApplication.ClockTimeInfo.visible=true;
				SetClockPlace(this.result.landlordpos);	
				gpltStartTime=new Date();
				isClock=true;*/
				ShowClockAndCallBack(autoSendCardWhenTimeOut);
				//将底牌绘画上去
				drawThreeCards(this.result.threecards.split(","));		
				//清除放弃		
				FlexGlobals.topLevelApplication.imgPlayerLeftReady.visible=false;
				FlexGlobals.topLevelApplication.imgPlayerRightReady.visible=false;
				FlexGlobals.topLevelApplication.imgPlayerDownReady.visible=false;	
				//清除放弃		
				FlexGlobals.topLevelApplication.imgDiscardDown.visible=false;
				FlexGlobals.topLevelApplication.imgDiscardLeft.visible=false;
				FlexGlobals.topLevelApplication.imgDiscardRight.visible=false;	
				//如果当前地主是自己
				if(this.result.landlordpos==this.selfPos){
					//自己必须出牌，不能不出
					this.mustSend=true;
					this.isMyTurn=true;
					FlexGlobals.topLevelApplication.landlordheader.x=140;
					FlexGlobals.topLevelApplication.landlordheader.y=500;
					//将地主的底牌加入手中
					var allcards:Array=this.PlayerCards.concat();
					for(var i:int=0;i<ThreeCardsObject.length;i++){
						allcards.push(ThreeCardsObject[i]);
						this.PlayerCards.push(ThreeCardsObject[i]);
					}
					
					drawPlayerCards(allcards,false);
					//显示出牌三按钮
					FlexGlobals.topLevelApplication.GameCardsCommand.visible=true;	
				}else{
					if(this.result.landlordpos==this.leftpos){
						FlexGlobals.topLevelApplication.landlordheader.x=5;
						FlexGlobals.topLevelApplication.landlordheader.y=269;
						//更新手牌
						var allcards:Array=this.PlayerCardsLeft.concat();
						for(var i:int=0;i<ThreeCardsObject.length;i++){						
							allcards.push(ThreeCardsObject[i]);
						}
						drawPlayerHandCards(true,allcards,false);
					}else if (this.result.landlordpos==this.rightpos){
						FlexGlobals.topLevelApplication.landlordheader.x=750;
						FlexGlobals.topLevelApplication.landlordheader.y=250;
						//更新手牌
						var allcards:Array=this.PlayerCardsRight.concat();
						for(var i:int=0;i<ThreeCardsObject.length;i++){						
							allcards.push(ThreeCardsObject[i]);
						}
						drawPlayerHandCards(false,allcards,false);
					}
					isMyTurn=false;
				}
				//根据landlordpos绘画地主图标
				FlexGlobals.topLevelApplication.landlordheader.visible=true;	
				//绘制本局叫分低分:3
				FlexGlobals.topLevelApplication.Label_GameScoreAndMultiple.visible=true;	
				//倍数：1
				FlexGlobals.topLevelApplication.Label_GameScoreAndMultiple.text="底分："+this.result.maxcallscore+"    倍数："+this.result.mutilple;
				
			}
		}
		public function testSendCards():void{
			/*var cards:Array = new Array();
			for each(var card:Card in this.PlayerCardsObject)
			{
				if(card.selected){
					cards.push(card.cardid);
					card.isSelectedtoPost = true;
				}
			}*/
		/*	var data:String = "";
			for(var id:int=0;id<cards.length;id++)
			{
				data += cards[id].toString();
				if(id != cards.length-1)
					data += ",";
			}	*/	
			var checkarr:Array = new Array();
			var cards:String=FlexGlobals.topLevelApplication.txt_input.text;
			if(cards!=""){
				checkarr=FlexGlobals.topLevelApplication.txt_input.text.split(",");
				//绘画前面出的牌
				drawAnyCards(checkarr,300,20);
			}
			if(GameObjectManager.Instance.checkCardtobePlayed(checkarr.sort(Array.NUMERIC))){
//				drawPlayedDownCards(checkarr);
				trace("可以出牌");
				Alert.show("可以出牌");
			}else{
				trace("不可以出牌");
				Alert.show("不可以出牌");
			}
				
//			controller.ControllerManager.Instance.debug("选择了牌："+data,true,true);	
		}
		public function sendCardsClicked(event:Event):void
		{
//			FlexGlobals.topLevelApplication.btnSendCards.enabled = false;			
			var checkarr:Array =lastCard.concat();
			if(GameObjectManager.Instance.checkCardtobePlayed(checkarr.sort(Array.NUMERIC))){
				// 发送出牌的消息，如果失败，将会自动重发，
				var cards:Array = new Array();
				for each(var card:Card in this.PlayerCardsObject)
				{
					if(card.selected){
						cards.push(card.cardid);
						card.isSelectedtoPost = true;
					}
				}
				var selectedCardIndex:int=CardPattern.Instance.patternCheck(cards.sort(Array.NUMERIC));	
				var data:String = "";
				for(var id:int=0;id<cards.length;id++)
				{
					data += cards[id].toString();
					if(id != cards.length-1)
						data += ",";
				}
			
				SocketManager.Instance.send(this.CreateOutMessage(BaseController.FIGHT_LANDLORD,"GAME_SNED_CARDS",data+"~"+selectedCardIndex));
				FlexGlobals.topLevelApplication.GameCardsCommand.visible=false;
				delePostedCards();
				drawPlayedDownCards(cards);
//				FlexGlobals.topLevelApplication.btnSendCards.enabled = false;
				
				//				drawPlayedDownCards(checkarr);
				/*trace("可以出牌");
				Alert.show("可以出牌");*/
			}else{
				trace("不可以出牌");
//				Alert.show("不可以出牌");
			}
			
			// 清空计数器
			//	Gamestate.StateSendCards.Instance.clearCounter();
			//客户端做出牌算法比较，排序，服务端只管接受转达

		}
		
		public function discardClicked(event:Event):void
		{
			if(this.mustSend){
				Alert.show("轮到你必须出牌！");
				return;
			}
			SocketManager.Instance.send(this.CreateOutMessage(BaseController.FIGHT_LANDLORD,"GAME_SNED_CARDS","pass~-1"));
			FlexGlobals.topLevelApplication.GameCardsCommand.visible=false;
//			FlexGlobals.topLevelApplication.imgDiscardDown.visible=true;
//			FlexGlobals.topLevelApplication.btnDiscard.enabled = false;
			//	Game.Instance.pass();
		}	
		public function ShowClockAndCallBack(func:Function=null):void{
			//显示倒计时闹钟在正确位置，时间重设30，通知taskloop可以倒计时
			FlexGlobals.topLevelApplication.ClockTimeInfo.visible=true;
			SetClockPlace(this.curPos);	
			gpltStartTime=new Date();
			isClock=true;
			this.timerCallBackFun=func;
		}
		public function testHint():void{
			GameObjectManager.Instance.deselectAllCards();
//			return;
			controller.ControllerManager.Instance.state=4;
//			ShowClockAndCallBack();
			//显示闹钟，并回调d
			var checkarr:Array = new Array();
			var cards:String=FlexGlobals.topLevelApplication.txt_input.text;
			if(cards!=""){
			 checkarr=FlexGlobals.topLevelApplication.txt_input.text.split(",");
			 //绘画前面出的牌
			 drawAnyCards(checkarr,300,20);
			}
			
			// 判断这次的要压的牌和最近的一次是不是一样
			var flag:Boolean = false;
			if(checkarr.length == lastCard.length && checkarr.length != 0) 
			{
				if( int(checkarr[0]/4) == int(lastCard[0]/4) && int(checkarr[checkarr.length-1]/4) == int(lastCard[lastCard.length-1]/4) )
				{
					flag = true;
				}
				
			}
			//如果此次已检查
			if(flag)
			{
				hintTimes++;
				if(hintCards.length <= hintTimes){
					hintTimes = 0;				
				}			
				GameObjectManager.Instance.selectCards(hintCards[hintTimes]);
			
			}
			else if(!flag && checkarr.length!=0){
				lastCard = checkarr.concat();
				hintCards = GameObjectManager.Instance.showHintCards(checkarr);
				//为了调试，将提示的卡牌绘制在左出牌位置
			/*	var allhints:Array=new Array();
				for(var i:int=0;i<hintCards.length;i++){
					allhints.push(hintCards[i]);
				}
				drawPlayerDownCards(true,allhints);*/
				if(hintCards.length > 0){
					hintTimes = 0;
					GameObjectManager.Instance.selectCards(hintCards[hintTimes]);
				}
				else{
					
					if(PlayerCards.length== hintTimes){				
						hintTimes=0;
					}					
//					hintCards.push(PlayerCards[hintTimes]);
					GameObjectManager.Instance.deselectAllCards();
					GameObjectManager.Instance.selectCards(checkarr.concat(PlayerCards[hintTimes]));
					hintTimes++;
				}
				}else{
					if(PlayerCards.length== hintTimes){				
						hintTimes=0;
					}	
					
					//					hintCards.push(PlayerCards[hintTimes]);
					GameObjectManager.Instance.deselectAllCards();
					GameObjectManager.Instance.selectCards(checkarr.concat(PlayerCards[hintTimes]));
					hintTimes++;
				}
//				hintCards.splice(0);	
		
			/*方块3 :(3-3)*4+1-1 =0
			梅花3:(3-3)*4+2-1=1
				红桃3 :(3-3)*4+3-1=2
				黑桃3:(3-3)*4+4-1=3
				
				方块4 :(4-3)*4+1-1 =4
				梅花4:(4-3)*4+2-1=5
				红桃4 :(4-3)*4+3-1=6
				黑桃4:(4-3)*4+4-1=7
				
				方块j :(11-3)*4+1-1 =32
					梅花j:(11-3)*4+2-1=33
						红桃j :(11-3)*4+3-1=34
							黑桃j:(11-3)*4+4-1=35*/
			// 检测该次的出牌是否符合要求，能否出牌。
			
			/*checkarr=lastCard.concat();
			hintCards = GameObjectManager.Instance.showHintCards(checkarr);
			if(hintCards.length > 0){
				hintTimes = 0;
				GameObjectManager.Instance.selectCards(hintCards[hintTimes]);
			}*/
		}
		
		//绘画指定的牌，在指定的地方
		public function drawAnyCards(allcards:Array,standx:int=0,standy:int=0):void{	
			GameObjectManager.Instance.removeCardfromScreen("AnyCard");
			allcards.sort(Array.NUMERIC);
			allcards.reverse();
			var rt:Rectangle;	
			rt = new Rectangle(0,0,GameController.cardsWidth,GameController.cardsHeight);
			for(var i:int=0; i<allcards.length; i++)	{
				var x:int=standx-(allcards.length*GameController.cardsIntervalX)/2+i*GameController.cardsIntervalX;
				var pt:Point = new Point(x,standy);														
				GameObjectManager.Instance.addCardtoScreen(allcards[i], "AnyCard", pt);
				
			}
		}	
		public function autoSendCardWhenTimeOut():void{
			return;
			if(this.curPos==this.selfPos){
				if(this.isCallScore){
					SocketManager.Instance.send(this.CreateOutMessage(BaseController.FIGHT_LANDLORD,"GAME_CALL_SCORE","0"));
					FlexGlobals.topLevelApplication.GameScoreCommand.visible=false;
					return;
				}
				if(this.mustSend){
					//自动出最小的牌
					(Card(PlayerCardsObject[0])).isSelectedtoPost=true;					
					SocketManager.Instance.send(this.CreateOutMessage(BaseController.FIGHT_LANDLORD,"GAME_SNED_CARDS",(Card(PlayerCardsObject[0])).cardid.toString()+"~1"));
					//				PlayerCardsObject				
					var cards:Array=new Array();
					cards.push((Card(PlayerCardsObject[0])).cardid);
					drawPlayedDownCards(cards);
					delePostedCards();
					FlexGlobals.topLevelApplication.GameCardsCommand.visible=false;
					trace("自动出牌！");
				}else{
					SocketManager.Instance.send(this.CreateOutMessage(BaseController.FIGHT_LANDLORD,"GAME_SNED_CARDS","pass~-1"));
					FlexGlobals.topLevelApplication.GameCardsCommand.visible=false;
					trace("pass！");
					SoundManager.Instance().playSendCardSound(0,0,1);//不要
				}
			}
			else{
				if(this.isCallScore){
					trace("对方倒计时结束！,没有叫分");
				}else{
					trace("对方倒计时结束！");
				}
				
			}
		/*	if(this.isMyTurn){
				if(this.mustSend){
					//自动出最小的牌
					(Card(PlayerCardsObject[0])).isSelectedtoPost=true;					
					SocketManager.Instance.send(this.CreateOutMessage(BaseController.FIGHT_LANDLORD,"GAME_SNED_CARDS",(Card(PlayerCardsObject[0])).cardid.toString()));
					//				PlayerCardsObject
					delePostedCards();
					var cards:Array=new Array();
					cards.push((Card(PlayerCardsObject[0])).cardid);
					drawPlayedDownCards(cards);
					FlexGlobals.topLevelApplication.GameCardsCommand.visible=false;
					trace("自动出牌！");
				}else{
					SocketManager.Instance.send(this.CreateOutMessage(BaseController.FIGHT_LANDLORD,"GAME_SNED_CARDS","pass"));
					FlexGlobals.topLevelApplication.GameCardsCommand.visible=false;
					trace("pass！");
					SoundManager.Instance().playSendCardSound(0,0,1);//不要
				}
			}
			else{
				trace("对方倒计时结束！");
			}*/
			
		}
		public function delePostedCards():void{
			// 删除被打出的牌
			var temp1:Array = new Array();
			var temp2:Array = new Array();
			//					var delcards:Array = new Array();
			for(var i:int=0; i<PlayerCardsObject.length; i++){
				if( !Card(PlayerCardsObject[i]).isSelectedtoPost){
					temp1.push(PlayerCards[i]);
					temp2.push(PlayerCardsObject[i]);					
				}
			   else
				{
					//							delcards.push(PlayerCardsObject[i]);
					GameObjectManager.Instance.removeSingleCardfromScreen(PlayerCardsObject[i], "Card");
				}
			}
			PlayerCards.splice(0);
			PlayerCardsObject.splice(0);
			PlayerCards = temp1.concat();
			PlayerCardsObject = temp2.concat();					
			
			// 修改剩下的牌的位置
			for(var i:int=0;i<PlayerCards.length; i++)
			{
				Card(PlayerCardsObject[i]).x = cardStandardX-(PlayerCards.length*cardsIntervalX/2)+i*cardsIntervalX;
			}
			FlexGlobals.topLevelApplication.Label_leftcardsnumDown.text=this.PlayerCards.length+"张";
			//牌全部出完
			trace("还剩:"+PlayerCardsObject.length+"张牌！");
			if(PlayerCardsObject.length==0){
				SocketManager.Instance.send(this.CreateOutMessage(BaseController.FIGHT_LANDLORD,"GAME_WIN_OVER","win"));
				trace("发送了GAME_WIN_OVER通知");
			}
		}
		public function deleLeftOrRightPostedCards(isLeft:Boolean,playCards:Array):void{
			
			if(isLeft){		
				//数组里删除出国的牌
				for(var i:int=0;i<playCards.length;i++){
					for(var j:int=0;j<PlayerCardsLeft.length;j++){
						if(PlayerCardsLeft[j]==playCards[i]){
							PlayerCardsLeft.splice(j,1);
							break;
						}
					}
				}
				//重新绘画手中的牌
//				drawPlayerDownCards(true,this.PlayerCardsLeft);
				GameObjectManager.Instance.removeOthersfromScreen("HandCardLeft");
				for(var k:int=0; k<PlayerCardsLeft.length; k++)
				{
					var pt:Point  = new Point(leftCardback_x,leftCardback_y-(PlayerCardsLeft.length*cardsIntervalY/2)+k*cardsIntervalY);			
					GameObjectManager.Instance.addCardtoScreen(PlayerCardsLeft[k], "HandCardLeft", pt,true);
				}
				trace("left play:"+playCards+". "+this.PlayerCardsLeft.length+"张");
				FlexGlobals.topLevelApplication.Label_leftcardsnumLeft.text=this.PlayerCardsLeft.length+"张";
			}else{
				//数组里删除出国的牌
				for(var i:int=0;i<playCards.length;i++){
					for(var j:int=0;j<PlayerCardsRight.length;j++){
						if(PlayerCardsRight[j]==playCards[i]){
							PlayerCardsRight.splice(j,1);
							break;
						}
					}
				}
				GameObjectManager.Instance.removeOthersfromScreen("HandCardRight");
				for(var k:int=0; k<PlayerCardsRight.length; k++)
				{
					var pt:Point  = new Point(rightCardback_x,rightCardback_y-(PlayerCardsRight.length*cardsIntervalY/2)+k*cardsIntervalY);				
					GameObjectManager.Instance.addCardtoScreen(PlayerCardsRight[k], "HandCardRight", pt,true);
				}
				trace("right play:"+playCards+". "+this.PlayerCardsRight.length+"张");
				FlexGlobals.topLevelApplication.Label_leftcardsnumRight.text=this.PlayerCardsRight.length+"张";
				
			}			
			//牌全部出完
//			if(PlayerCardsObject.length==0){
//				SocketManager.Instance.send(this.CreateOutMessage(BaseController.FIGHT_LANDLORD,"GAME_WIN","win"));
//			}
		}
		public function btnHint_clickHandler(event:MouseEvent):void
		{	
			//显示闹钟，并回调d
			var gameCards:Array=PlayerCards.concat();
			gameCards.reverse();
			var checkarr:Array;	
			GameObjectManager.Instance.deselectAllCards();
			if(this.mustSend){//提示单牌，轮流玩家自己出牌
				checkarr=new Array();
				if(PlayerCards.length<hintTimes){				
					hintTimes=0;
				}	
				//hintCards.push(PlayerCards[hintTimes]);	
				while(hintTimes>0&&hintTimes<PlayerCards.length&&int(gameCards[hintTimes]/4)==int(gameCards[hintTimes-1]/4)){
					hintTimes++;
				}
				GameObjectManager.Instance.selectCards(checkarr.concat(gameCards[hintTimes]));
				hintTimes++;
				return;
			}else{
				checkarr=lastCard.concat();	
			}
			// 判断这次的要压的牌和最近的一次是不是一样
			var flag:Boolean = false;
			if(checkarr.length == lastCkeckCard.length && checkarr.length != 0) 
			{
				if( int(checkarr[0]/4) == int(lastCkeckCard[0]/4) && int(checkarr[checkarr.length-1]/4) == int(lastCkeckCard[lastCard.length-1]/4) )
				{
					flag = true;
				}
				
			}
			//如果此次已检查
			if(flag)
			{
				
				if(hintCards.length < hintTimes){
					hintTimes = 0;				
				}
//				GameObjectManager.Instance.deselectAllCards();
				GameObjectManager.Instance.selectCards(hintCards[hintTimes]);
				hintTimes++;
				
			}
			else if(!flag && checkarr.length!=0){
				lastCkeckCard = checkarr.concat();
				hintCards = GameObjectManager.Instance.showHintCards(checkarr);
				//为了调试，将提示的卡牌绘制在左出牌位置
			/*	var allhints:Array=new Array();
				for(var i:int=0;i<hintCards.length;i++){
					allhints.push(hintCards[i]);
				}
				drawPlayerDownCards(true,allhints);*/
				if(hintCards.length > 0){
					hintTimes = 0;
					GameObjectManager.Instance.selectCards(hintCards[hintTimes]);
				}
				/*else{
					
					if(PlayerCards.length== hintTimes){				
						hintTimes=0;
					}					
					//					hintCards.push(PlayerCards[hintTimes]);
//					GameObjectManager.Instance.deselectAllCards();
					GameObjectManager.Instance.selectCards(checkarr.concat(PlayerCards[hintTimes]));
					hintTimes++;
				}*/
			}else{
				
			}
		}
		public function sendMessage():void{
		  var msg:String=FlexGlobals.topLevelApplication.txt_input.text;
		  if(msg==""){
			  return;
		  }
		 var color:uint=FlexGlobals.topLevelApplication.color_picker.selectedColor;
		 SocketManager.Instance.send(this.CreateOutMessage(BaseController.FIGHT_LANDLORD,"GAME_SNED_MESSAGE",msg+"~"+color));
		 FlexGlobals.topLevelApplication.chat_txt.validateNow();
		 FlexGlobals.topLevelApplication.txt_input.text="";
		/* var fmt:DateFormatter = new DateFormatter();
		 fmt.formatString = "yyyy-MM-dd HH:mm:ss";
		 FlexGlobals.topLevelApplication.chat_txt.htmlText+="<font  color='#" + rgbToHex(color) +"'>" +LoginController.Instance.userId+" "+fmt.format(new Date())+":"+msg+"</font><br>";
		 FlexGlobals.topLevelApplication.chat_txt.verticalScrollPosition=FlexGlobals.topLevelApplication.chat_txt.maxVerticalScrollPosition;
		 FlexGlobals.topLevelApplication.txt_input.text="";*/
		}
		private function rgbToHex(color:uint):String{
			var c:String = "00000" + color.toString(16)
			var e:int = c.length;
			c = c.substring(e - 6, e);
			return c.toUpperCase();
		}
		//接收到其他玩家的聊天消息
		private function GAME_RECEIVER_MESSAGE_NOTIFY_HANDLER(event:SocketEvent):void{
			//{msg:“11”,color:12}
			controller.ControllerManager.Instance.debug("接收到派发消息：GAME_RECEIVER_MESSAGE_NOTIFY_HANDLER"+event.results[1]+",MSG:"+event.results[3],true);
			if(this.receive(event.results)){
				FlexGlobals.topLevelApplication.chat_txt.validateNow();
				FlexGlobals.topLevelApplication.chat_txt.htmlText+=this.result.user+" "+this.result.date+"<br>"+"<font  color='#" + rgbToHex(this.result.color) +"'>" + this.result.msg+"</font><br>";
				FlexGlobals.topLevelApplication.chat_txt.verticalScrollPosition=FlexGlobals.topLevelApplication.chat_txt.maxVerticalScrollPosition; 
			
			}
		}
		
		private function GAME_CALL_SCORE_NOTIFY_HANDLER(event:SocketEvent):void{
			//{"currentpos":2,"lastpost":1,"iscall":false,"score":0,maxcallscore:2,maxCallScorePos:2,islandlord:true}
			controller.ControllerManager.Instance.debug("接收到派发消息：GAME_CALL_SCORE_NOTIFY_HANDLER"+event.results[1]+",MSG:"+event.results[3],true);
			if(this.receive(event.results)){	
				SoundManager.Instance().playCallScoreSound(this.result.score);
				callScoreCount++;
				if(this.result.currentpos==4){					
					FlexGlobals.topLevelApplication.GameScoreCommand.visible=false;
					return;
				}
				//闹钟显示在当前人上面
				//显示倒计时闹钟在正确位置，时间重设30，通知taskloop可以倒计时
				this.curPos=this.result.currentpos;		
				if(this.result.currentpos==this.selfPos){
					//显示叫分按钮，并且监听
					switch(this.result.maxcallscore){
						case 0:
							break;
						case 1:
							FlexGlobals.topLevelApplication.GameScoreCommand1.enabled=false;
							break;
						case 2:
							FlexGlobals.topLevelApplication.GameScoreCommand1.enabled=false;
							break;
						FlexGlobals.topLevelApplication.GameScoreCommand1.enabled=false;
						FlexGlobals.topLevelApplication.GameScoreCommand2.enabled=false;
						break;
						case 3:
							FlexGlobals.topLevelApplication.GameScoreCommand1.enabled=false;
							FlexGlobals.topLevelApplication.GameScoreCommand2.enabled=false;
							FlexGlobals.topLevelApplication.GameScoreCommand3.enabled=false;
							break;
					}
					//如果小于三，说明可以在含分，如果对于3，则说明该玩家就是地主，服务端会通知
					if(this.result.maxcallscore<3){
					FlexGlobals.topLevelApplication.GameScoreCommand.visible=true;
					FlexGlobals.topLevelApplication.GameScoreCommand3.addEventListener(MouseEvent.CLICK,GAME_CALL_SCORE);
					FlexGlobals.topLevelApplication.GameScoreCommand2.addEventListener(MouseEvent.CLICK,GAME_CALL_SCORE);
					FlexGlobals.topLevelApplication.GameScoreCommand1.addEventListener(MouseEvent.CLICK,GAME_CALL_SCORE);
					FlexGlobals.topLevelApplication.GameScoreCommandbujiao.addEventListener(MouseEvent.CLICK,GAME_CALL_SCORE);
					ShowClockAndCallBack(null);
					/*	function():void{
							SocketManager.Instance.send(this.CreateOutMessage(BaseController.FIGHT_LANDLORD,"GAME_CALL_SCORE","0"));
							FlexGlobals.topLevelApplication.GameScoreCommand.visible=false;
						});	*/	
					}
				}else{
					ShowClockAndCallBack(null);
				}
				if(this.result.lastpost==this.leftpos){					
					if(!this.result.iscall){
						//显示放弃，并播放声音
						FlexGlobals.topLevelApplication.imgDiscardLeft.visible=true;
						
					}else
					{
					//播放分数声音this.result.score
						
					}
					
				}else if(this.result.lastpost==this.rightpos){					
					if(!this.result.iscall){
						//显示放弃，并播放声音
						FlexGlobals.topLevelApplication.imgDiscardRight.visible=true;
					}else
					{
						//播放分数声音this.result.score
						
					}
				}
				else if(this.result.lastpost==this.selfPos){					
					if(!this.result.iscall){
						//显示放弃，并播放声音
						FlexGlobals.topLevelApplication.imgDiscardDown.visible=true;
					}else
					{
						//播放分数声音this.result.score
						
					}
				}
			}
		}
		private function GAME_MUST_SEND_NOTIFY_HANDLER(event:SocketEvent):void{
			//{currentpos:2,lastpost:1,issend:false,lastcards:'3,5',cardindex:1},这样说明1,3号桌子没有出牌，pass，如自己2号，则最后牌A是自己出的
			controller.ControllerManager.Instance.debug("接收到派发消息：GAME_MUST_SEND_NOTIFY_HANDLER"+event.results[1]+",MSG:"+event.results[3],true);
			if(this.receive(event.results)){
				SoundManager.Instance().playSendCardSound(this.result.cardindex,this.result.lastcards);
				this.curPos=this.result.currentpos;
//				this.lastCard.splice(0);
				this.lastCkeckCard.splice(0);
				//清除所有的出牌记录，以及pass图标
				GameObjectManager.Instance.removeCardfromScreen("PlayedCardLeft");
				GameObjectManager.Instance.removeCardfromScreen("PlayedCardRight");		
				GameObjectManager.Instance.removeCardfromScreen("PlayedCardDown");		
				//放弃提示
				FlexGlobals.topLevelApplication.imgDiscardLeft.visible=false;
				FlexGlobals.topLevelApplication.imgDiscardDown.visible=false;
				FlexGlobals.topLevelApplication.imgDiscardRight.visible=false;
				if(this.result.currentpos==this.selfPos){
					this.lastCard.splice(0);
					mustSend=true;
					//显示出牌三按钮按钮
					FlexGlobals.topLevelApplication.GameCardsCommand.visible=true;	//	
				}else{
					mustSend=false;
					isMyTurn=false;
				}
				if(this.result.lastcards=="pass"){					
					SoundManager.Instance().playSendCardSound(0,0,1);//不要							
					if(this.result.lastpost==this.leftpos){
						FlexGlobals.topLevelApplication.imgDiscardLeft.visible=true;
					}else if(this.result.lastpost==this.rightpos){
						FlexGlobals.topLevelApplication.imgDiscardRight.visible=true;
					}	
					else if(this.result.lastpos==this.selfPos){
						FlexGlobals.topLevelApplication.imgDiscardDown.visible=true;
					}
				}else{
					if(this.result.lastpost!=this.selfPos){
						lastCard=this.result.lastcards.split(",");
						if(this.result.lastpost==this.leftpos){
							deleLeftOrRightPostedCards(true,lastCard);
							drawPlayerDownCards(true,this.result.lastcards.split(","));					
						}else{
							deleLeftOrRightPostedCards(false,lastCard);
							drawPlayerDownCards(false,this.result.lastcards.split(","));					
						}				
					}
					if(this.result.cardindex>=3&&this.result.cardindex<=7){
						this.mutil=this.mutil*2;
					}
				}
				
				//显示倒计时闹钟在正确位置，时间重设30，通知taskloop可以倒计时
				ShowClockAndCallBack(autoSendCardWhenTimeOut);			
				
			}
		}
		public function SetClockPlace(pos:int):Boolean{	
			if(pos==this.selfPos){
				FlexGlobals.topLevelApplication.ClockTimeInfo.x=320;
				FlexGlobals.topLevelApplication.ClockTimeInfo.y=300;
				return true;
			}else if(pos==this.leftpos){
				FlexGlobals.topLevelApplication.ClockTimeInfo.x=70;
				FlexGlobals.topLevelApplication.ClockTimeInfo.y=100;
				return true;
				
			}else if(pos==this.rightpos){
				FlexGlobals.topLevelApplication.ClockTimeInfo.x=520;
				FlexGlobals.topLevelApplication.ClockTimeInfo.y=100;
				return true;
			}		
			return false;
		}
		private function GAME_TURNTO_PALY_NOTIFY_HANDLER(event:SocketEvent):void{		
			//{currentpos:2,lastpost:1,issend:false,lastcards:'3VA',cardindex:1},这样说明1号桌子没有出牌，pass，如自己2号，则最后牌A是3号出的		
			/*SetClockPlace(this.result.currentpos);
			isClock=true;*/
			controller.ControllerManager.Instance.debug("接收到派发消息：GAME_TURNTO_PALY_NOTIFY_HANDLER"+event.results[1]+",MSG:"+event.results[3],true);
			if(this.receive(event.results)){
				mustSend=false;
				//播放炸弹音效
//				if(this.result.cardindex>=3&&this.result.cardindex<=7){
				SoundManager.Instance().playSendCardSound(this.result.cardindex,this.result.lastcards);
//				}
				// if it's my turn to play , show the send button
				this.curPos=this.result.currentpos;
				if(this.result.currentpos==this.selfPos){
					GameObjectManager.Instance.removeCardfromScreen("PlayedCardDown");		
					FlexGlobals.topLevelApplication.GameCardsCommand.visible=true;
				}
				
				if(this.result.lastcards!="pass"){
//					if(this.result.lastpost!=this.selfPos){
						lastCard=this.result.lastcards.split(",");
						if(this.result.lastpost==this.leftpos){
							deleLeftOrRightPostedCards(true,lastCard);
							drawPlayerDownCards(true,this.result.lastcards.split(","));
							//更新手牌
//							deletePlayerHandCards(true,this.result.lastcards.split(","));
						}else if(this.result.lastpost==this.rightpos){
							deleLeftOrRightPostedCards(false,lastCard);
							drawPlayerDownCards(false,this.result.lastcards.split(","));
//							deletePlayerHandCards(false,this.result.lastcards.split(","));
						}
//						drawPlayerDownCards(true,this.result.lastcards.split(","));			
//					}	
						//处理翻倍
						if(this.result.cardindex>=3&&this.result.cardindex<=7){
							this.mutil=this.mutil*2;
						}
					
				}
				if(this.result.lastcards=="pass")
				{
					SoundManager.Instance().playSendCardSound(0,0,1);//不要
			
						
						if(this.result.lastpost==this.leftpos){
							FlexGlobals.topLevelApplication.imgDiscardLeft.visible=true;
							GameObjectManager.Instance.removeCardfromScreen("PlayedCardLeft");
						}else if(this.result.lastpost==this.rightpos){
							FlexGlobals.topLevelApplication.imgDiscardRight.visible=true;
						}	
					    else if(this.result.lastpos==this.selfPos){
						FlexGlobals.topLevelApplication.imgDiscardDown.visible=true;
						GameObjectManager.Instance.removeCardfromScreen("PlayedCardRight");
					}
					
				}				
				//显示倒计时闹钟在正确位置，时间重设30，通知taskloop可以倒计时
				ShowClockAndCallBack(autoSendCardWhenTimeOut);
				/*FlexGlobals.topLevelApplication.ClockTimeInfo.visible=true;
				SetClockPlace(this.result.currentpos);	
				gpltStartTime=new Date();
				isClock=true;*/
			}
		}
		private function GAME_INTERRUPTED_OTHER_DISCONNECT_HANDLER(event:SocketEvent):void{
			//currentpos
			trace("接收到派发消息：GAME_INTERRUPTED_OTHER_DISCONNECT_HANDLER"+event.results[1]+",MSG:"+event.results[3]);
			controller.ControllerManager.Instance.debug("接收到派发消息：GAME_INTERRUPTED_OTHER_DISCONNECT_HANDLER"+event.results[3]);
						if(this.receive(event.results)){
							//放弃提示
							FlexGlobals.topLevelApplication.imgDiscardLeft.visible=false;
							FlexGlobals.topLevelApplication.imgDiscardDown.visible=false;
							FlexGlobals.topLevelApplication.imgDiscardRight.visible=false;
							FlexGlobals.topLevelApplication.btnReady.visible=true;		
							FlexGlobals.topLevelApplication.GameCardsCommand.visible=false;
							FlexGlobals.topLevelApplication.ClockTimeInfo.visible=false;
							this.isClock=false;
							this.mustSend=false;
							ControllerManager.Instance.state=1;//join room,but not ready!
							this.timerCallBackFun=null;
							//drawother hand card		
							trace("摊牌，清除左手玩家的桌牌");
							GameObjectManager.Instance.removeOthersfromScreen("HandCardLeft");
							// 对得到的牌进行排序并显示			
							trace("摊牌，左手玩家桌牌数："+PlayerCardsLeft.length);
							for(var k:int=0; k<PlayerCardsLeft.length; k++)
							{
								var pt:Point  = new Point(leftCardback_x,leftCardback_y-(PlayerCardsLeft.length*cardsIntervalY/2)+k*cardsIntervalY);
								// 传入的是左上的位置坐标
								//						GameObjectManager.Instance.setSpecCardVisible(PlayerCardsRight[k], "HandCardRight", pt, cardback_BaseZOrder+k, true);
								//
								trace("摊牌，绘制左手玩家的桌牌："+PlayerCardsLeft[k]);
								GameObjectManager.Instance.addCardtoScreen(PlayerCardsLeft[k], "HandCardLeft", pt);
							}
							FlexGlobals.topLevelApplication.Label_leftcardsnumLeft.text=this.PlayerCardsLeft.length+"张";
							
							GameObjectManager.Instance.removeOthersfromScreen("HandCardRight");
							trace("摊牌，清除右手玩家的桌牌");
							trace("摊牌，左手玩家桌牌数："+PlayerCardsRight.length);
							// 对得到的牌进行排序并显示
							PlayerCardsRight.reverse();
							for(var k:int=0; k<PlayerCardsRight.length; k++)
							{
								var pt:Point  = new Point(rightCardback_x,rightCardback_y-(PlayerCardsRight.length*cardsIntervalY/2)+k*cardsIntervalY);
								// 传入的是左上的位置坐标
								//						GameObjectManager.Instance.setSpecCardVisible(PlayerCardsRight[k], "HandCardRight", pt, cardback_BaseZOrder+k, true);
								//						pt = null;
								trace("摊牌，绘制右手玩家的桌牌："+PlayerCardsRight[k]);
								GameObjectManager.Instance.addCardtoScreen(PlayerCardsRight[k], "HandCardRight", pt);
							}
							FlexGlobals.topLevelApplication.Label_leftcardsnumRight.text=this.PlayerCardsRight.length+"张";
							//{currentpos}
							if(this.result.currentpos==this.leftpos){
								FlexGlobals.topLevelApplication.playerinfoLeft.visible=false;
								FlexGlobals.topLevelApplication.Label_leftcardsnumLeftl.visible=false;
								FlexGlobals.topLevelApplication.Label_leftcardsnumLeft.visible=false;
								FlexGlobals.topLevelApplication.imgPlayerLeftReady.visible=false;
							}else if(this.result.currentpos==this.rightpos){
								FlexGlobals.topLevelApplication.playerinfoRight.visible=false;
								FlexGlobals.topLevelApplication.Label_leftcardsnumRightl.visible=false;
								FlexGlobals.topLevelApplication.Label_leftcardsnumRight.visible=false;
								FlexGlobals.topLevelApplication.imgDiscardRight.visible=false;
							}
						}
		}
		private function GAME_GAME_STARTED_HANDLER(event:SocketEvent):void{
			// 3~1333962918406~GAME_GAME_STARTED~1VA,4VA,4VK,1V9,3V2,1V4,2V5,1V6,3VJ,3V9,1V3,4V4,3VA,4V3,2V7,2V10,1V7~0=17,1=17,2=17
			//3~1333962918406~GAME_GAME_STARTED~4V8,4V10,3VK,2V6,4VQ,3V4,2VA,2VQ,2V9,3V3,3V7,2VK,2V2,1VQ,4VJ,4V5,4V6~0=17,1=17,2=17
			trace("接收到派发消息：GAME_GAME_STARTED_HANDLER"+event.results[1]+",MSG:"+event.results[3]);
			controller.ControllerManager.Instance.debug("接收到派发消息：GAME_GAME_STARTED_HANDLER"+event.results[3]);
			ControllerManager.Instance.state=4;
			FlexGlobals.topLevelApplication.inGame=true;	
			GameObjectManager.Instance.startup();
			var allcards:Array=event.results[3].toString().split(",");
			drawPlayerCards(allcards);		
		}
		private function GAME_FIRST_PLAY_HANDLER(event:SocketEvent):void{
			//{"firstpos":2,"1":{"cards":"12,121","cardnumber":17}}
			//3~1333962918406~GAME_FIRST_PLAY~2~4V8,4V10,3VK,2V6,4VQ,3V4,2VA,2VQ,2V9,3V3,3V7,2VK,2V2,1VQ,4VJ,4V5,4V6~0=17,1=17,2=17;1VA,4VA,4VK,1V9,3V2,1V4,2V5,1V6,3VJ,3V9,1V3,4V4,3VA,4V3,2V7,2V10,1V7~0=17,1=17,2=17;1VK,3V8,3VQ,4V2,0VY,1VJ,1V5,4V9,3V10,4V7,0VX,2V3,2VJ,1V2,1V8,3V6,1V10~0=17,1=17,2=17;
		
			trace("接收到派发消息：GAME_FIRST_PLAY_HANDLER"+event.results[1]+",MSG:"+event.results[3]);
			controller.ControllerManager.Instance.debug("接收到派发消息：GAME_FIRST_PLAY_HANDLER"+event.results[3]);
						if(this.receive(event.results)){
							ControllerManager.Instance.state=4;
							this.lastCard.slice(0);
							this.mutil=1;
							this.gamestate=1;
							isCallScore=true;
							FlexGlobals.topLevelApplication.inGame=true;	
							FlexGlobals.topLevelApplication.imgPlayerDownReady.visible=false;	
							FlexGlobals.topLevelApplication.imgPlayerLeftReady.visible=false;	
							FlexGlobals.topLevelApplication.imgPlayerRightReady.visible=false;	
							FlexGlobals.topLevelApplication.landlordheader.visible=false;
							GameObjectManager.Instance.removeCardfromScreen("threeCard");
							//剩余牌数字
							FlexGlobals.topLevelApplication.Label_leftcardsnumDownl.visible=true;
							FlexGlobals.topLevelApplication.Label_leftcardsnumRightl.visible=true;
							FlexGlobals.topLevelApplication.Label_leftcardsnumLeftl.visible=true;
							FlexGlobals.topLevelApplication.Label_leftcardsnumDown.visible=true;
							FlexGlobals.topLevelApplication.Label_leftcardsnumRight.visible=true;
							FlexGlobals.topLevelApplication.Label_leftcardsnumLeft.visible=true;
							FlexGlobals.topLevelApplication.Label_leftcardsnumDown.text="17张";
							FlexGlobals.topLevelApplication.Label_leftcardsnumRight.text="17张";
							FlexGlobals.topLevelApplication.Label_leftcardsnumLeft.text="17张";
							GameObjectManager.Instance.startup();
							// self
							if(this.result.hasOwnProperty(this.selfPos)){
								drawPlayerCards(this.result[this.selfPos].cards.split(","));
							}
							if(this.result.hasOwnProperty(this.leftpos)){
								drawPlayerHandCards(true,this.result[this.leftpos].cards.split(","));
							}
							if(this.result.hasOwnProperty(this.rightpos)){
								drawPlayerHandCards(false,this.result[this.rightpos].cards.split(","));
							}
						
							var firstpos:int=this.result.firstpos;
							this.curPos=this.result.firstpos;
							if(firstpos==this.selfPos){								
								//显示叫分按钮，并且监听
								this.mustSend=true;
								this.isMyTurn=true;
								FlexGlobals.topLevelApplication.GameScoreCommand.visible=true;
								FlexGlobals.topLevelApplication.GameScoreCommand3.addEventListener(MouseEvent.CLICK,GAME_CALL_SCORE);
								FlexGlobals.topLevelApplication.GameScoreCommand2.addEventListener(MouseEvent.CLICK,GAME_CALL_SCORE);
								FlexGlobals.topLevelApplication.GameScoreCommand1.addEventListener(MouseEvent.CLICK,GAME_CALL_SCORE);
								FlexGlobals.topLevelApplication.GameScoreCommandbujiao.addEventListener(MouseEvent.CLICK,GAME_CALL_SCORE);
								//将剩余三张牌加加入手中
								
								
							}else if(firstpos==this.leftpos){
								
							}else if(firstpos==this.rightpos){
								
							}							
							//显示倒计时闹钟在正确位置，时间重设30，通知taskloop可以倒计时
							ShowClockAndCallBack(autoSendCardWhenTimeOut);
						/*	FlexGlobals.topLevelApplication.ClockTimeInfo.visible=true;
							SetClockPlace(this.result.currentpos);	
							gpltStartTime=new Date();
							isClock=true;*/
							//如果不是自己，则显示倒计时，在对应人的头上，如果是自己 ，出现叫分按钮
						}
		}
		
		public function showhidenClockTime(isVisible:Boolean):void{
			FlexGlobals.topLevelApplication.sandglass_clock.visible=isVisible;
			FlexGlobals.topLevelApplication.sandglass_count10.visible=isVisible;
			FlexGlobals.topLevelApplication.sandglass_count1.visible=isVisible;
		}
		public function testDrawplayerHander():void{
			GameObjectManager.Instance.startup();
			var leftarray:Array="1VA,4VA,4VK,1V9,3V2,1V4,2V5,1V6,3VJ,3V9,1V3,4V4,3VA,4V3,2V7,2V10,1V7".split(",");
			var rightarray:Array="4V8,4V10,3VK,2V6,4VQ,3V4,2VA,2VQ,2V9,3V3,3V7,2VK,2V2,1VQ,4VJ,4V5,4V6".split(",");
			var selarray:Array="1VK,3V8,3VQ,4V2,0VY,1VJ,1V5,4V9,3V10,4V7,0VX,2V3,2VJ,1V2,1V8,3V6,1V10,1VA,4VA,4VK,1V9,3V2,1V4,2V5,1V6,3VJ,3V9,1V3,4V4,3VA,4V3,2V7,2V10,1V7,4V8,4V10,3VK,2V6,4VQ,3V4,2VA,2VQ,2V9,3V3,3V7,2VK,2V2,1VQ,4VJ,4V5,4V6".split(",");
			drawPlayerHandCards(true,leftarray);
			drawPlayerHandCards(false,rightarray);
			drawPlayerCards(selarray);
			// 显示其他家的牌堆
			GameObjectManager.Instance.enterFrame();
		}
		// 控制显示所有的卡牌背面
		public function showCardback(flag:Boolean):void
		{
		 if(flag)
			{
			GameObjectManager.Instance.addOtherstoScreen(ResourceManagerPoker.cardback1, "HandCardLeft", new Point(leftCardback_x,leftCardback_y));
			GameObjectManager.Instance.addOtherstoScreen(ResourceManagerPoker.cardback1, "HandCardRight", new Point(rightCardback_x,rightCardback_y));
			}
			else
			{
			GameObjectManager.Instance.removeOthersfromScreen("HandCardLeft");
			GameObjectManager.Instance.removeOthersfromScreen("HandCardRight");
			}
		}
		/**
		 * 描画除自己以外的玩家的打出的牌
		 * @param obj
		 * 	 传入的数据对象
		 * @param pos
		 * 	需要描画的玩家的座位位置编号
		 */
		public function drawPlayedDownCards(allcards:Array):void{
			var gpname:String="PlayedCardDown";	
			GameObjectManager.Instance.removeCardfromScreen(gpname);			
			allcards.sort(Array.NUMERIC);
			allcards.reverse();	
			for(var k:int=0; k<allcards.length; k++)
			{
				var x:int=GameController.playedCardStdX-(PlayerCards.length*GameController.cardsIntervalX)/2+k*GameController.cardsIntervalX;
				var pt:Point = new Point(x,playedCardStdY);	
				GameObjectManager.Instance.addCardtoScreen(allcards[k],gpname, pt);
			}		
		}	
		/**
		 * 描画除自己以外的玩家的打出的牌
		 * @param obj
		 * 	 传入的数据对象
		 * @param pos
		 * 	需要描画的玩家的座位位置编号
		 */
		public function drawPlayerDownCards(isLeft:Boolean,allcards:Array):void{
			if(allcards.length==0||(allcards.length==1&&allcards[0]=="")){
				trace("打出的牌是空的："+allcards);
				return;
			}
			var gpname:String="PlayedCardLeft";
			var x:int=playedleftCardStdX;
			var y:int=playedleftCardStdY;
			if(!isLeft){
				gpname="PlayedCardRight";
				x=playedrightCardStdX;
				y=playedrightCardStdY;
			}
				GameObjectManager.Instance.removeCardfromScreen(gpname);			
//				allcards.sort(Array.NUMERIC);
//				allcards.reverse();	
				for(var k:int=0; k<allcards.length; k++)
				{
					var pt:Point  = new Point(x,y-(allcards.length*cardsIntervalY/2)+k*cardsIntervalY);					
					GameObjectManager.Instance.addCardtoScreen(allcards[k],gpname, pt);
				}		
		}	
		
		public function deletePlayerHandCards(isLeft:Boolean,allcards:Array):void{
			if(isLeft){
				
				// 对得到的牌进行排序并显示
//				PlayerCardsLeft.splice(0);
				for(var i:int=0; i<PlayerCardsLeft.length; i++){
					for(var j:int=0; j<allcards.length; j++){
						if(PlayerCardsLeft[i]==allcards[j]){
							PlayerCardsLeft.splice(i,1);
							break;
						}
					}
				}
				
				PlayerCardsLeft.sort(Array.NUMERIC);
				PlayerCardsLeft.reverse();	
				GameObjectManager.Instance.removeOthersfromScreen("HandCardLeft");
				for(var k:int=0; k<PlayerCardsLeft.length; k++)
				{
					var pt:Point  = new Point(leftCardback_x,leftCardback_y-(PlayerCardsLeft.length*cardsIntervalY/2)+k*cardsIntervalY);
					// 传入的是左上的位置坐标
					//						GameObjectManager.Instance.setSpecCardVisible(PlayerCardsRight[k], "HandCardRight", pt, cardback_BaseZOrder+k, true);
					//						pt = null;
					GameObjectManager.Instance.addCardtoScreen(PlayerCardsLeft[k], "HandCardLeft", pt,true);
				}
				FlexGlobals.topLevelApplication.Label_leftcardsnumLeft.text=this.PlayerCardsLeft.length+"张";
			}else{
				GameObjectManager.Instance.removeOthersfromScreen("HandCardRight");
				// 对得到的牌进行排序并显示
				for(var i:int=0; i<PlayerCardsRight.length; i++){
					for(var j:int=0; j<allcards.length; j++){
						if(PlayerCardsRight[i]==allcards[j]){
							PlayerCardsRight.splice(i,1);
							break;
						}
					}
				}	
				for(var i:int=0; i<allcards.length; i++){
					PlayerCardsRight.push(getCardByName(allcards[i]));
				}					
				// do sort
				PlayerCardsRight.sort(Array.NUMERIC);
				PlayerCardsRight.reverse();
				for(var k:int=0; k<PlayerCardsRight.length; k++)
				{
					var pt:Point  = new Point(rightCardback_x,rightCardback_y-(PlayerCardsRight.length*cardsIntervalY/2)+k*cardsIntervalY);
					// 传入的是左上的位置坐标
					//						GameObjectManager.Instance.setSpecCardVisible(PlayerCardsRight[k], "HandCardRight", pt, cardback_BaseZOrder+k, true);
					//						pt = null;
					GameObjectManager.Instance.addCardtoScreen(PlayerCardsRight[k], "HandCardRight", pt,true);
				}
				FlexGlobals.topLevelApplication.Label_leftcardsnumRight.text=this.PlayerCardsRight.length+"张";
			}
		}	
		/**
		 * 描画除自己以外的玩家的手牌
		 * @param obj
		 * 	 传入的数据对象
		 * @param pos
		 * 	需要描画的玩家的座位位置编号
		 */
		public function drawPlayerHandCards(isLeft:Boolean,allcards:Array,needConvert:Boolean=true):void{
			if(isLeft){
				GameObjectManager.Instance.removeOthersfromScreen("HandCardLeft");
				// 对得到的牌进行排序并显示
				PlayerCardsLeft.splice(0);
				if(needConvert){
					for(var i:int=0; i<allcards.length; i++){
						PlayerCardsLeft.push(getCardByName(allcards[i]));
					}
				}else{
					for(var i:int=0; i<allcards.length; i++){
						PlayerCardsLeft.push(allcards[i]);
					}
				}			
				PlayerCardsLeft.sort(Array.NUMERIC);
				PlayerCardsLeft.reverse();	
				for(var k:int=0; k<PlayerCardsLeft.length; k++)
				{
					var pt:Point  = new Point(leftCardback_x,leftCardback_y-(PlayerCardsLeft.length*cardsIntervalY/2)+k*cardsIntervalY);
					// 传入的是左上的位置坐标
					//						GameObjectManager.Instance.setSpecCardVisible(PlayerCardsRight[k], "HandCardRight", pt, cardback_BaseZOrder+k, true);
					//						pt = null;
					GameObjectManager.Instance.addCardtoScreen(PlayerCardsLeft[k], "HandCardLeft", pt,true);
				}
				trace("绘画左家手牌数："+PlayerCardsLeft.length+"."+PlayerCardsLeft);
				FlexGlobals.topLevelApplication.Label_leftcardsnumLeft.text=this.PlayerCardsLeft.length+"张";
			}else{
				GameObjectManager.Instance.removeOthersfromScreen("HandCardRight");
				// 对得到的牌进行排序并显示
				PlayerCardsRight.splice(0);
				if(needConvert){
					for(var i:int=0; i<allcards.length; i++){
						PlayerCardsRight.push(getCardByName(allcards[i]));
					}
				}else{
					for(var i:int=0; i<allcards.length; i++){
						PlayerCardsRight.push(allcards[i]);
					}
				}
									
				// do sort
				PlayerCardsRight.sort(Array.NUMERIC);
				PlayerCardsRight.reverse();
				for(var k:int=0; k<PlayerCardsRight.length; k++)
				{
					var pt:Point  = new Point(rightCardback_x,rightCardback_y-(PlayerCardsRight.length*cardsIntervalY/2)+k*cardsIntervalY);
					// 传入的是左上的位置坐标
					//						GameObjectManager.Instance.setSpecCardVisible(PlayerCardsRight[k], "HandCardRight", pt, cardback_BaseZOrder+k, true);
					//						pt = null;
					GameObjectManager.Instance.addCardtoScreen(PlayerCardsRight[k], "HandCardRight", pt,true);
				}
				trace("绘画右家手牌数："+PlayerCardsRight.length+"."+PlayerCardsRight);
				FlexGlobals.topLevelApplication.Label_leftcardsnumRight.text=this.PlayerCardsRight.length+"张";
			}
		}	
		//绘画三张底牌
		public function drawThreeCards(allcards:Array):void{
			ThreeCardsObject.splice(0);
			for(var i:int=0; i<allcards.length; i++){
				ThreeCardsObject.push(getCardByName(allcards[i]));
			}
			ThreeCardsObject.sort(Array.NUMERIC);
			ThreeCardsObject.reverse();
			var rt:Rectangle;	
			rt = new Rectangle(0,0,GameController.cardsWidth,GameController.cardsHeight);
			for(var i:int=0; i<ThreeCardsObject.length; i++)	{
				var x:int=400-(ThreeCardsObject.length*GameController.cardsIntervalX)/2+i*GameController.cardsIntervalX;
				var pt:Point = new Point(x,cardThreeStandardY);														
				GameObjectManager.Instance.addCardtoScreen(ThreeCardsObject[i], "threeCard", pt);
				
			}
		}	
		
		public function drawPlayerCards(allcards:Array,isNeedConvert:Boolean=true):void{
			PlayerCards.splice(0);
			PlayerCardsObject.splice(0);
			if(isNeedConvert){
				for(var i:int=0; i<allcards.length; i++){
					PlayerCards.push(getCardByName(allcards[i]));					
				}
			}else{
				for(var i:int=0; i<allcards.length; i++){					
					PlayerCards.push(allcards[i]);
				}
			}
		
			PlayerCards.sort(Array.NUMERIC);
			PlayerCards.reverse();
			trace("allcards所有牌长度"+allcards.length+"："+allcards);
			trace("地主牌长度："+PlayerCards.length+"："+PlayerCards);
			GameObjectManager.Instance.removeCardfromScreen("HandCardDown");
			var rt:Rectangle;	
			rt = new Rectangle(0,0,GameController.cardsWidth,GameController.cardsHeight);		
			for(var i:int=0; i<PlayerCards.length; i++)	{
				var x:int=GameController.cardStandardX-(PlayerCards.length*GameController.cardsIntervalX)/2+i*GameController.cardsIntervalX;
				var pt:Point = new Point(x,cardStandardY);														
				PlayerCardsObject.push(GameObjectManager.Instance.addCardtoScreen(PlayerCards[i], "HandCardDown", pt));
				
			}
			FlexGlobals.topLevelApplication.Label_leftcardsnumDown.text=this.PlayerCards.length+"张";		
		}	
	
		private function GAME_GAME_WAIT_HANDLER(event:SocketEvent):void{
			trace("接收到派发消息：GAME_GAME_WAIT_HANDLER"+event.results[1]+",MSG:"+event.results[3]);
			controller.ControllerManager.Instance.debug("接收到派发消息：GAME_GAME_WAIT_HANDLER"+event.results[3]);
			if(this.receive(event.results)){
				
			}
		}
	
		//游戏被服务器端创建，收到通知
		private function GAME_GAME_CREATE_HANDLER(event:SocketEvent):void
		{
			SocketManager.Instance.removeEventListener(SocketEvent.GAME_GAME_CREATE,GAME_GAME_CREATE_HANDLER);
			controller.ControllerManager.Instance.debug("接收到派发消息：GAME_GAME_CREATE_HANDLER"+event.results[3]);
			trace("接收到派发消息：GAME_GAME_CREATE_HANDLER"+event.results[1]+",MSG:"+event.results[3]);
			if(this.receive(event.results)){
			
//				ControllerManager.Instance.state=2;
//				FlexGlobals.topLevelApplication.imgPlayerDownReady.visible=true;
			}
		}
		
		public function getCardByName(name:String):int{
			/*方块3 :(3-3)*4+1-1 =0
				梅花3:(3-3)*4+2-1=1
				红桃3 :(3-3)*4+3-1=2
				黑桃3:(3-3)*4+4-1=3
				
			方块4 :(4-3)*4+1-1 =4
			梅花4:(4-3)*4+2-1=5
			红桃4 :(4-3)*4+3-1=6
			黑桃4:(4-3)*4+4-1=7
			
				方块j :(11-3)*4+1-1 =32
				梅花j:(11-3)*4+2-1=33
				红桃j :(11-3)*4+3-1=34
				黑桃j:(11-3)*4+4-1=35*/
			if(name.indexOf("V")!=-1){
				var results:Array=name.split("V");
				var cardtype:int=int(results[0]);
				var cardvalue:String=results[1];
				var index:int;
				switch(cardvalue){
					case "3":
					case "4":
					case "5":
					case "6":
					case "7":
					case "8":
					case "9":
					case "10":	
					index=(int(cardvalue)-3)*4+cardtype-1;
					break;
					case "J":
					     index=32+cardtype-1;
					     break;
					case "Q":
						index=36+cardtype-1;
						break;
					case "K":
						index=40+cardtype-1;
						break;
					case "A":
						index=44+cardtype-1;
						break;
					case "2":
						index=48+cardtype-1;
						break;
					case "X":
						index=52;
						break;
					case "Y":
						index=53;
						break;
				}
			
				return index;
			
				
			}
			else
			{
			return -1;;
			}
		}
		public function sortCards(obj:Object):Array
		{
			var j:int;
			var arr:Array = new Array();
			for(j=0;j<obj.cards[selfPos].number;j++)
			{
				arr.push(obj.cards[selfPos].card[j]);
			}
			// do sort
			arr.sort(Array.NUMERIC);
			arr.reverse();
			return arr;
		}
	
	}
}